package com.lfh.itos.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

/**
 * 公共方法类，目前有的方法
 * 
 * 1.获取流水号getSerialNumber
 * 
 * 2.获取一个随机数randomNum
 * 
 * 3.获取ip地址 getIpAddr
 * @author bing.zhan
 *
 */
public final class CommonUtils {

	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyyMMddHHmmssS");
	private static Random rnd = new Random();

	/**
	 * 获取流水号
	 * 
	 * @param random
	 *           时间随机数后位数
	 * @return yyyyMMddHHmmssS+random位的随机数
	 */
	public static String getSerialNumber(int random) {
		Date date = new Date();
		String serialNumber;
		if (random == 0) {
			serialNumber = sdf.format(date);
		} else {
			serialNumber = sdf.format(date) + randomNum(random);
		}
		return serialNumber;
	}

	/**
	 * 获取流水号 时间随机数后位数
	 * 
	 * @return yyyyMMddHHmmssS+4位的随机数
	 */
	public static String getSerialNumber() {
		Date date = new Date();
		String serialNumber;
		serialNumber = sdf.format(date) + randomNum(4);
		return serialNumber;
	}

	/**
	 * 获取一个随机数
	 * 
	 * @param digCount
	 *            随机数位
	 * @return 随机数字符串
	 */
	public static String randomNum(int digCount) {
		StringBuilder sb = new StringBuilder(digCount);
		for (int i = 0; i < digCount; i++)
			sb.append((char) ('0' + rnd.nextInt(10)));
		return sb.toString();
	}

	/**
	 * 返回浏览器的IP地址<br/>
	 * 请求有可能经过apache代理模式，equest.getRemoteAddr()返回的不一定是浏览器的IP地址
	 * 
	 * @param request
	 *            HttpServletRequest实例
	 * @return 浏览器的IP地址
	 */
	public final static String getIpAddr(HttpServletRequest request) {
		String address = request.getHeader("X-Forwarded-For");
		if (address != null) {
			int iFirstDot = address.indexOf(',');
			if (iFirstDot > 0) {
				address = address.substring(0, iFirstDot);
			}
			if (isIPv4Address(address)) {
				return address;
			}
		}
		return request.getRemoteAddr();
	}
	
	/**
	 * 是否是 ipv4地址
	 * 
	 * @param s
	 *            字符串
	 * @return
	 */
	public final static boolean isIPv4Address(String str) {
		if (str != null) {
			int dot1 = str.indexOf('.');
			if (dot1 <= 0) {
				return false;
			}
			int temp;
			try {
				temp = Integer.parseInt(str.substring(0, dot1++));
				if (temp < 0 || temp > 255) {
					return false;
				}
			} catch (Exception ex) {
				return false;
			}

			int dot2 = str.indexOf('.', dot1);
			if (dot2 <= 0) {
				return false;
			}
			try {
				temp = Integer.parseInt(str.substring(dot1, dot2++));
				if (temp < 0 || temp > 255) {
					return false;
				}
			} catch (Exception ex) {
				return false;
			}

			int dot3 = str.indexOf('.', dot2);
			if (dot3 <= 0) {
				return false;
			}
			try {
				temp = Integer.parseInt(str.substring(dot2, dot3++));
				if (temp < 0 || temp > 255) {
					return false;
				}
			} catch (Exception ex) {
				return false;
			}
			try {
				temp = Integer.parseInt(str.substring(dot3));
				if (temp < 0 || temp > 255) {
					return false;
				}
			} catch (Exception ex) {
				return false;
			}
			return true;
		}
		return false;
	}
	public static String getUserId(HttpServletRequest request){
		return null;
	}
}
